<?php
class IndexAction extends BaseAction {
 

	function index() {
	 //  $_SESSION['openid']=isset($_GET['openid'])?$_GET['openid']:$_SESSION['openid']; 
	// $_SESSION['openkey']=isset($_GET['openkey'])?$_GET['openkey']:$_SESSION['openkey'];
	 //  $this->getmall_order();

	  
		$this->display();
		}
   
   // linkageSelfanlimall 异步获取商城数据
   function register($username,$webid,$web){
 
	  //INSERT INTO `duoduo_user` (`id`, `ddusername`, `ddpassword`, `realname`, `regtime`, `regip`, `loginnum`, `lastlogintime`, `lasttixian`, `alipay`, `email`, `money`, `jifenbao`, `jifen`, `txstatus`, `dhstate`, `level`, `tjr`, `tjr_over`, `mobile`, `mobile_test`, `qq`, `pass_question`, `pass_answer`, `yitixian`, `fxb`, `jihuo`, `hart`, `tbyitixian`, `ucid`, `hytx`, `signtime`, `tenpay`, `bank_name`, `tbtxstatus`, `txtool`, `bank_code`) VALUES
//(1, 'ebuy361', 'f4b477237d342b0d6f72b8c984a33722', NULL, '2013-08-26 15:29:35', '127.0.0.1', 1, '2013-08-26 15:29:35', 0, NULL, '326815462@qq.com', 0.00, 0.00, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0.00, 0, 1, 0, 0.00, 0, 0, 0, NULL, NULL, 0, 1, '0');
     $data['ddusername']=$username;
	 $data['ddpassword']=md5('13631520471');
	 $data['regtime']=date('Y-m-d H:i:s');
	 $data['regip']=get_client_ip();
	 $data['loginnum']=1;
	 $data['lastlogintime']=date('Y-m-d H:i:s');
	 $data['jihuo']=1;
	 $data1['webid']=$webid;
	 $data1['web']=$web;
	 $data1['webname']=$username;
	if( $this->user->add($data)){
	 $data2=$this->user->field('id')->where("ddusername='".$data['ddusername']."'")->select();
	 cookie('userid',$data2[0]['id'],3600*24*7);

	 $this->apilogin->add($data1);
	// dump($this->apilogin->select());
   redirect('/');
	 }
 
   }
 
 
    
	public function slogin($type = null){
		empty($type) && $this->error('参数错误');

		//加载ThinkOauth类并实例化一个对象
		import("ORG.ThinkSDK.ThinkOauth");
		$sns  = ThinkOauth::getInstance($type);

		//跳转到授权页面
		redirect($sns->getRequestCodeURL());
	}

	//授权回调地址
	public function callback($type = null, $code = null){
		(empty($type) || empty($code)) && $this->error('参数错误');
		
		//加载ThinkOauth类并实例化一个对象
		import("ORG.ThinkSDK.ThinkOauth");
		$sns  = ThinkOauth::getInstance($type);

		//腾讯微博需传递的额外参数
		$extend = null;
		if($type == 'tencent'){
			$extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
		}

		//请妥善保管这里获取到的Token信息，方便以后API调用
		//调用方法，实例化SDK对象的时候直接作为构造函数的第二个参数传入
		//如： $qq = ThinkOauth::getInstance('qq', $token);
		$token = $sns->getAccessToken($code , $extend);
          
		//获取当前登录用户信息
		if(is_array($token)){
		   
		  $user1=$this->apilogin->where("webid='".$token['openid']."' AND web='".$type."'")->select();
		  if(count($user1)>0){
		   cookie('userid',$user1[0]['uid'],3600*24*7);
			redirect('/');
		  }
		  else{
		  $user_info = A('Type', 'Event')->$type($token);
		  //dump($user_info);
		 // echo $user_info['nick'];
		   $this->register($user_info['nick'],$token['openid'],$type);
            }
		}
	}
}